package serova.valeriya.electricity;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;




public class Result {
	static Configuration config = new Configuration();	
	private static int previousRegistrationForStat;	
	private static String dateFirst;
	private static String dateLast;
	public	static List <String> getStatisticSpecificationBills(String dateFirst, String dateLast, List <String> fileContent)throws ParseException{
		List <String> statisticInformation = new ArrayList <String>();	
		statisticInformation = fileContent.subList(2, fileContent.size());
		List <String> statistic = new ArrayList <String>();		
		String[] array = null;
		Date dateFromFile = new Date(System.currentTimeMillis());
		Date dateF = new Date(System.currentTimeMillis());
		Date dateL = new Date(System.currentTimeMillis());		
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");			
		for (String bill : statisticInformation) {
			array = AnalysisTariffData.getDataArray(bill);
			dateFromFile = df.parse(array[0]);
			dateF = (Date)df.parse(dateFirst);
			dateL = (Date)df.parse(dateLast);			
			int i = dateFromFile.compareTo(dateF);
			int j = dateFromFile.compareTo(dateL);
			if (i >= 0&j<=0){
				 statistic.add(bill);				
			}		
	}
		return statistic;
	}
	
	public static List <StatisticData> getStatisticDataObjects(List <String> statistic){
		List <StatisticData> statisticData = new ArrayList <StatisticData>();		
		String date = null;		
		String metterReadings = null;
		String electricity = null;
		String sum = null;
		String[] array = null;
		for (String dataBill : statistic) {
			array = AnalysisTariffData.getDataArray(dataBill);
			date = (array[0]);
			metterReadings = (array[1]);
			electricity = (array[2]);
			sum = (array[3]);
			StatisticData oneStatisticData = new StatisticData();
			oneStatisticData.setDate(date);
			oneStatisticData.setMetterReadings(metterReadings);
			oneStatisticData.setElectricity(electricity);
			oneStatisticData.setSum(sum);			
			statisticData.add(oneStatisticData);
		}
		return statisticData;
	}
	
	public static void makeStatistic()throws IOException, ParseException{
		UserDialog.printReport(UserDialog.getDateFirstRequest());
		dateFirst = UserDialog.getString();
		UserDialog.printReport(UserDialog.getDateLastRequest());
		dateLast = UserDialog.getString();	
		List <String> billStatistic = getStatisticSpecificationBills(dateFirst, dateLast, PowerBillApp.getFileContent());					
		InputStreamReader reportReader = TariffDataSource.getInputStreamReader(config.getStatTemplateName());
		String templeteReport = ReportIOUtil.getFileInformation(reportReader);						
		Writer out = new PrintWriter(config.getStatReportName());						
		String previousRegistrationDataSource = billStatistic.get(billStatistic.size()-2);
		String[] array = AnalysisTariffData.getDataArray(previousRegistrationDataSource);
		previousRegistrationForStat = Integer.parseInt(array[1]);									
		ReportUtils.doStatistic(templeteReport, dateFirst, dateLast, previousRegistrationForStat, billStatistic, out);					
		ReportIOUtil.writeResultToFile(out);	
		UserDialog.printReport(UserDialog.getStatResultInfo());			
	}
	
	public static void makeReport()throws IOException{
		UserDialog.printReport(UserDialog.getCurrentRegistrationRequest());				
		PowerBillApp.getInput().setCurrentRegistration(UserDialog.getInteger());		
		double payment = PowerBillApp.calculatePayment(PowerBillApp.getInput());
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		String p = nf.format(payment);		
		InputStreamReader reportReader = TariffDataSource.getInputStreamReader(config.getTemplateName());
		String templeteReport = ReportIOUtil.getFileInformation(reportReader);						
		Writer out = new PrintWriter(config.getReportName());			
		ReportUtils.doReport(templeteReport, PowerBillApp.getInput(), payment, out);			
		ReportIOUtil.writeResultToFile(out);
		UserDialog.printReport(UserDialog.getReportResultInfo());			
		UserDialog.printReport(UserDialog.getDataValidityCheck());		
		int i = UserDialog.getInteger();	
		  
		   if (i==1){
			   BufferedWriter buffered = PowerBillApp.getBufferedWriter(config.getRegistrationDataPath());
			   String data = PowerBillApp.getDate()+" "+PowerBillApp.getInput().getCurrentRegistration()+" "+PowerBillApp.getInput().calculateElectricity()+" "+p;
			   PowerBillApp.writeToFile(buffered, data);
			     
		   } else {
			   System.exit(0);
		   }		
	}

	public static int getPreviousRegistrationForStat() {
		return previousRegistrationForStat;
	}

	public static String getDateFirst() {
		return dateFirst;
	}

	public static String getDateLast() {
		return dateLast;
	}	
}



